package com.tnxst.phexamination.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;


import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.fragment.PackageServiceFragment;

import java.util.List;

public class PackageServiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private List<ServicePackageDTO> signPackages;

    private ChangedData changedData;

    private FragmentManager fragmentManager;

    private PackageServiceFragment fragment;




    public PackageServiceAdapter(List<ServicePackageDTO> signPackages, FragmentManager fragmentManager) {
        this.signPackages = signPackages;
        this.fragmentManager = fragmentManager;
    }

    public void setSignPackages(List<ServicePackageDTO> signPackages) {
        this.signPackages = signPackages;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = View.inflate(parent.getContext(), R.layout.service_package_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        holders.name.setText(signPackages.get(position).getSERVICENAME());
        holders.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(signPackages.get(position));
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView name;

        View view;


        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.textView97);
            view = itemView;
        }
    }

    @Override
    public int getItemCount() {
        return signPackages.size();
    }

    public interface ChangedData{
        void changedData();
    }

    public void setOnClickChangedData(ChangedData changedData){
        this.changedData = changedData;
    }

    private void showFragment(ServicePackageDTO signPackage) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
        fragment = PackageServiceFragment.newInstance();
        Bundle bundle = new Bundle();
        bundle.putSerializable("servicePackage", signPackage);
        fragment.setArguments(bundle);
        ft.replace(android.R.id.content, fragment);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
